<template>
  <body id="poster">
  <el-form class="login-container" label-position="left" label-width="0px">
    <h1 class="login_title">考试系统登录</h1>
    <el-form-item>
      <el-input type="text" v-model="registerForm.name" placeholder="姓名"></el-input>
    </el-form-item>

    <el-form-item>
      <el-input type="text" v-model="registerForm.username" placeholder="账号"></el-input>
    </el-form-item>

    <el-form-item>
      <el-input type="password" v-model="registerForm.password" placeholder="密码"></el-input>
    </el-form-item>

    <el-form-item>
      <div style="display:inline-block;">
        <el-input placeholder="请输入验证码" v-model="registerForm.code" style="width: 50%"></el-input>
        <img
          style="border:1px;float:right;"
          :src="src" @click="getImage" :key="key"
          :fit="fit"></img>
      </div>
    </el-form-item>

    <el-form-item >
      <el-button type="primary" style="width: 48.5%;background: #505458;border: none" v-on:click="register">注册</el-button>
      <el-button type="primary" style="width: 48.5%;background: #505458;border: none" v-on:click="login">前往登录</el-button>
    </el-form-item>
  </el-form>
  </body>
</template>


<script>
  export default {
    created() {
      this.getImage();
    },
    data() {
      return {
        fit: 'fill',
        src:"",
        key:"",
        registerForm: {
          name: '',
          username: '',
          password: '',
          code: '',
        },
        responseResult: []
      }
    },
    methods: {
      login () {
        this.$router.push('/')
      },
      getImage(){   //获取验证码
        this.$http.get("/role/getImage").then((res)=>{
          this.src = "data:image/png;base64,"+res.data.image;
          this.key = res.data.key;
          this.t_code=res.data.true_code;
        });
      },
      register:async function() {
        if (this.t_code!=this.registerForm.code){
          this.$notify({
            title: 'Tips',
            message: '验证码输入有误',
            type: 'error',
            duration: 2000
          })
          this.getImage()
        }else if (this.t_code===this.registerForm.code){
          const {data:data} = await this.$http.post("/role/register",this.registerForm)
          if (data.code === 2000) {
            localStorage.setItem('authorization', data.data)
            this.$notify({
              title: 'Tips',
              message: '注册成功',
              type: 'success',
              duration: 2000
            })
            this.$router.push('/login')
          }else {
              this.$notify({
                title: 'Tips',
                message:data.msg,
                type: 'error',
                duration: 2000
              })
              this.getImage()
            }
          }
        }
      },
  }
</script>

<style>
  #poster {
    background:url("../assets/img0.jpg") no-repeat;
    background-position: center;
    height: 100%;
    width: 100%;
    background-size: cover;
    position: fixed;
  }
  body{
    margin: 0px;
    padding: 0;
  }

  .login-container {
    border-radius: 15px;
    background-clip: padding-box;
    margin: 90px auto;
    width: 350px;
    padding: 35px 35px 15px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
  }

  .login_title {
    margin: 0px auto 40px auto;
    text-align: center;
    color: #505458;
  }


</style>


